package gupao.one.danlimodle;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ContainerSingleton {

    private ContainerSingleton(){}

    private  static Map<Class,Object> IOC = new ConcurrentHashMap<>();

    public  static Object getInstance(Class<?> clazz){

        Object instance = null ;

        if(!IOC.containsKey(clazz)){
            synchronized (IOC){
                if(IOC.containsKey(clazz)){
                    return IOC.get(clazz);
                }
                try {
                    instance = clazz.newInstance() ;
                    IOC.put(clazz,instance);
                } catch (Exception e) {
                    System.out.println(e);
                }
            }
            return  instance ;
        }else{
            return IOC.get(clazz);
        }



    }


    public static void main(String[] args) {


        new Thread(new ContainerSingletonRunnable()).start();
        new Thread(new ContainerSingletonRunnable()).start();
        new Thread(new ContainerSingletonRunnable()).start();
        new Thread(new ContainerSingletonRunnable()).start();


    }
}
